/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2025-02-21 10:04:55
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-02-26 08:32:36
 * @FilePath: /vue3-admin-server/src/admin/v1/auth/dept.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Controller, UseGuards, Post, Body, Get, Param } from '@nestjs/common';
// import { ApiOperation } from '@nestjs/swagger';

import { AuthGuard } from 'src/guards/auth.guard';
import { DeptService } from 'src/shareModules/auth/auth.module';
import { ResponseService } from 'src/shareModules/common/common.module';
import { DeptDTO, DeptListDTO } from 'src/common/dto';
import { generateObjFromError } from 'src/common/utils';

@Controller('admin/api/v1/dept')
@UseGuards(AuthGuard)
export class DeptController {
    constructor(
        private readonly deptService: DeptService,
        private readonly responseService: ResponseService
    ) {}

    @Get('readDetail/:id')
    async readDetail(@Param() { id }: { id: number }) {
        const detail = await this.deptService.readDetail(id);
        return this.responseService.success(undefined, detail);
    }

    @Get('readTree')
    // @ApiOperation({ summary: '分页查询菜单' })
    async readTree(): Promise<VO.ResultVO<VO.DeptTreeVO[]>> {
        const data = await this.deptService.readTree();
        return this.responseService.success(undefined, data);
    }

    @Post('readList')
    // @ApiOperation({ summary: '分页查询菜单' })
    async readList(@Body() listDTO: DeptListDTO) {
        const data = await this.deptService.readList(listDTO);
        return this.responseService.success(undefined, data);
    }

    @Post('save')
    async save(@Body() itemDto: DeptDTO) {
        try {
            const id = await this.deptService.save(itemDto);
            return this.responseService.success(undefined, { id });
        } catch (e) {
            return this.responseService.error(
                undefined,
                generateObjFromError(e as Error)
            );
        }
    }

    @Post('delete/:id')
    async delete(@Param() { id }: { id: number }) {
        try {
            await this.deptService.delete(id);
            return this.responseService.success();
        } catch (e) {
            return this.responseService.error(
                undefined,
                generateObjFromError(e as Error)
            );
        }
    }
}
